package cn.ccit.DAOImp;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.PayOrderRecordItemDAO;
import cn.ccit.entites.record.DishOrderRecordItem;
import cn.ccit.entites.record.PayOrderRecordItem;
import cn.ccit.util.MyDBUtil;

public class PayOrderRecordItemDAOimp implements PayOrderRecordItemDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(PayOrderRecordItem.class);
	@Override
	public boolean addPayOrderRecordItem(PayOrderRecordItem cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("save PayOrderRecordItem fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
			
		}
		return false;
	}

	@Override
	public boolean removePayOrderRecordItem(PayOrderRecordItem cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove PayOrderRecordItem fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
			
		}
		return false;
	}

	@Override
	public boolean changePayOrderRecordItemInfo(PayOrderRecordItem cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("change PayOrderRecordItem fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
			
		}
		return false;
	}

	@Override
	public List<PayOrderRecordItem> getSomePayOrderRecordItems(int start,
			int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from PayOrderRecordItem").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomePayOrderRecordItems fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public PayOrderRecordItem searchPayOrderRecordItemByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem where PayOrderRecordItem.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				PayOrderRecordItem PayOrderRecordItem=(PayOrderRecordItem)query.list().get(0);
				session.beginTransaction().commit();
				return PayOrderRecordItem;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecordItem> searchPayOrderRecordItemByYear(String year) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem where PayOrderRecordItem.orderYear=?";
				Query query=session.createQuery(hql);
				query.setString(0, year);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by year fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecordItem> searchPayOrderRecordItemByMonth(
			String month) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem where PayOrderRecordItem.orderMonth=?";
				Query query=session.createQuery(hql);
				query.setString(0, month);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by month fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecordItem> searchPayOrderRecordItemByDate(String date) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem where PayOrderRecordItem.orderDate=?";
				Query query=session.createQuery(hql);
				query.setString(0, date);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by date fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecordItem> searchPayOrderRecordItemBySerialsNumber(
			Long SerialsNumber) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecordItem as PayOrderRecordItem where PayOrderRecordItem.serialsNumber=?";
				Query query=session.createQuery(hql);
				query.setLong(0, SerialsNumber);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by SerialsNumber fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public boolean batchAddPayOrderRecordItem(
			List<PayOrderRecordItem> list) {
		PayOrderRecordItem cdRecord=null;
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				for (int i = 0; i < list.size(); i++) {
					cdRecord=list.get(i);
					session.save(cdRecord);
					 // 以每50个数据作为一个处理单元
				    if(i%50==0)  
				    {
				        // 只是将Hibernate缓存中的数据提交到数据库，保持与数据库数据的同步
				        session.flush();  
				        // 清除内部缓存的全部数据，及时释放出占用的内存
				        session.clear();  
				    }
				}
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("batch save PayOrderRecordItem fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
			
		}
		return false;
	}
}
